<%@page errorPage="/auth/exceptionHandler.jsp"%>
<%@page import="java.util.ResourceBundle"%>
<%@page import="edu.ncsu.csc.itrust.enums.TransactionType"%>
<%@ page import="net.tanesha.recaptcha.ReCaptcha"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaImpl"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaResponse"%>

<%@include file="/global.jsp"%>

<%
	pageTitle = "iTrust - Login";
%>
<%
	String remoteAddr = request.getRemoteAddr();
	//recaptcha.properties file found in WEB-INF/classes (usually not seen in Eclipse)
	ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
	ResourceBundle reCaptchaProps = ResourceBundle
			.getBundle("edu.ncsu.csc.itrust.recaptcha");
	reCaptcha.setPrivateKey(reCaptchaProps.getString("api.key"));

	String challenge = request
			.getParameter("recaptcha_challenge_field");
	String uresponse = request.getParameter("recaptcha_response_field");

	String user = request.getParameter("j_username");
	String pass = request.getParameter("j_password");

	if (challenge != null) {
		ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
				remoteAddr, challenge, uresponse);

		if (reCaptchaResponse.isValid()) {
			loginFailureAction.setCaptcha(true);
			validSession = true;
			response.sendRedirect("/iTrust/j_security_check?j_username="
					+ user + "&j_password=" + pass);
		} else {
			if (request.getParameter("loginError") == null) {
				loginFailureAction.setCaptcha(false);
				loggingAction.logEvent(TransactionType.LOGIN_FAILURE,
						Long.parseLong(user), Long.parseLong(user), "");
				pageContext.forward("/login.jsp?loginError=true");
			}
		}
	} else if (loginFailureAction.needsCaptcha() && user != null) {
		loginFailureAction.setCaptcha(false);
	} else if (user != null
			&& !"true".equals(request.getParameter("loginError"))) {
		session.setAttribute("loginFlag", "true");
		response.sendRedirect("/iTrust/j_security_check?j_username="
				+ user + "&j_password=" + pass);
	}

	if (request.getParameter("loginError") != null) {
		loginMessage = loginFailureAction.recordLoginFailure();
		loggingAction.logEvent(TransactionType.LOGIN_FAILURE,
				Long.parseLong(user), Long.parseLong(user), "");
		response.sendRedirect("/iTrust/login.jsp");
	}
%>

<%@include file="/header.jsp"%>

<%
	if (!loginFailureAction.needsCaptcha()) {
%>
<div id="welcomeScreen">
<h1>Welcome to iTrust</h1>
<br/>
<h2>About Us</h2>
<br/>
<p id="aboutUs">iTrust is a medical application that provides patients with a means to 
keep up with their medical history and records as well as communicate 
with their doctors, including selecting which doctors to be their 
primary caregiver, seeing and sharing satisfaction results, and other 
tasks. iTrust is also an interface for medical staff from various
locations. iTrust allows the staff to keep track of their patients
through messaging capabilities, scheduling of office visits, diagnoses,
prescribing medication, ordering and viewing lab results, among other
funtions. Privacy and security of patient records are ensured, as
protected by the HIPAA statute.</p>
</div>
<%
	} else {
%>
<form method="post" action="/iTrust/login.jsp"><span>MID</span><br />
<input type="text" maxlength="10" name="j_username"><br />
<span>Password</span><br />
<input type="password" maxlength="20" name="j_password"><br />
<br />
<%
	ReCaptcha c = ReCaptchaFactory.newReCaptcha(
				"6Lcpzb4SAAAAAHCD9njojEQJE3ZFuRVYQDsHdZjr",
				"6Lcpzb4SAAAAAGbscE39L3UmHQ_ferVd7RyJuo5Y", false);
		out.print(c.createRecaptchaHtml(null, null));
%> <input type="submit" value="Login"><br />
<br />

<a style="font-size: 80%;" href="/iTrust/util/resetPassword.jsp">Reset
Password</a></form>

<%
	}
%>
<%@include file="/footer.jsp"%>

